/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JanelaResultadoPesquisa.java
 *
 * Created on 21/05/2011, 21:28:06
 */

package gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
import modelo.Lei;

/**
 *
 * @author Jackson
 */
public class JanelaResultadoPesquisa extends javax.swing.JFrame implements UI{

    /** Creates new form JanelaResultadoPesquisa */
    public JanelaResultadoPesquisa() {
        setTitle("Resultado da Pesquisa");
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        botaoAlterar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(tabela);

        botaoAlterar.setText("Alterar");
        botaoAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAlterarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(botaoAlterar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 459, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botaoAlterar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAlterarActionPerformed
        int index = getTabela().getSelectedRow();
        Lei l = new Lei();
        l.setNumeroLei(tabela.getValueAt(index, 0).toString());
        l.setProcesso(tabela.getValueAt(index, 1).toString());
        l.setEmenta(tabela.getValueAt(index, 2).toString());
        l.setTexto(tabela.getValueAt(index, 3).toString());
        String s = (tabela.getValueAt(index, 4).toString());
        l.setId(Long.parseLong(s));
        janelaModificarLei.setarCampos(l);
        janelaModificarLei.setVisible(true);
    }//GEN-LAST:event_botaoAlterarActionPerformed
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoAlterar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables
    Controlador controlador;
    JanelaModificarLei janelaModificarLei;

    public JanelaModificarLei getJanelaModificarLei() {
        return janelaModificarLei;
    }

    public void setJanelaModificarLei(JanelaModificarLei jml){
        janelaModificarLei = jml;
    }

    public Controlador getControlador() {
        return controlador;
    }

    public void setControlador(Controlador c) {
        controlador = c;
    }

    public void exibirMensagem(String mensagem) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void exibirMensagemErro(String mensagem) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mostrarObjeto(Object o) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void mostrarObjetos(List l) {
        montarTabela((ArrayList<Lei>) l);
    }

    private void montarTabela(ArrayList<Lei> leis) {
        tabela.setModel(new ModeloTabela(leis));
    }

    private JTable getTabela() {
        return tabela;
    }

    public class ModeloTabela extends AbstractTableModel{

        ArrayList<Lei> leis;

        ModeloTabela(ArrayList<Lei> p){
            leis = p;
        }

        public String getColumnName(int column){
            if(column == 0) return "Nº Lei";
            else if(column == 1) return "Processo";
            else if(column == 2) return "Ementa";
            else if(column == 3) return "Texto";
            else if(column == 4) return "ID";
            return null;
        }

        public int getColumnCount() {
            return 5;
        }

        public int getRowCount() {
            return leis.size();
        }

        public Object getValueAt(int row, int col) {
            Lei p = leis.get(row);
            if (col==0) return p.getNumeroLei();
            else if (col==1) return p.getProcesso();
            else if (col==2) return p.getEmenta();
            else if (col==3) return p.getTexto();
            else if (col==4) return p.getId();
            return null;
        }


    }
}
